﻿using Autofac;
using DDD.Core.DependencyInjection;
using DDD.Extensions;
using DDD.Grpc.Client.Test.Common;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using static DDD.Grpc.Main.UserService;

namespace DDD.Grpc.Client.Test
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("仅用于对Gprc Server端进行测试");
            Console.WriteLine("DDD.Grpc.Client.Test 运行中......");

            var host = new HostBuilder()
                .ConfigureServices((context, serviceCollection) =>
                {
                    serviceCollection.AddTransient<GrpcExceptionInterceptor>();

                    serviceCollection.AddGrpcClient<UserServiceClient>((serviceProvider, op) =>
                    {
                        string address = ConfigurationExtension.Configuration.Instance().GetValue<string>(AppSettingsConst.Grpc.ADDRESS)!;

                        op.Address = new Uri(address);
                    }).AddInterceptor<GrpcExceptionInterceptor>();
                })
                .ReplaceIConfiguration()
                .AddSerilog(ConfigurationExtension.Configuration.Instance())
                .AddAutofac((context, builder) =>
                {
                    builder.RegisterModule<StartupModule>();
                    builder.RegisterModule<AutofacIOC>();
                })
                .UseConsoleLifetime()
                .Build();

            await host.RunAsync();
        }
    }
}
